package com.mimiwang.oss.service.Imp;

import com.google.common.base.Strings;
import com.mimiwang.oss.service.FileUploadService;
import com.mimiwang.oss.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/4/9 17:48
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {

    private static List<String> blackList=new ArrayList<>(Arrays.asList(""));


    @Autowired
    Environment environment;

    @Value("${server.port}")
    String port;


    @Value("${spring.resources.static-locations}")
    String basePath;


    @Override
    public String localStore(MultipartFile file) {
        String fileName=file.getOriginalFilename();
        if (Strings.isNullOrEmpty(fileName)) {
            throw new NullPointerException("文件名为空");
        }
        String suffix = FileUtils.getSuffix(fileName);

        if(!isAllow(suffix)){
            return null;
        }

        String storePath = generateStorePath(file);

        try {
            file.transferTo(new File(storePath));
            return generateUrl(file);

        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public List<String> localStoreFiles(MultipartFile[] files) {
        ArrayList<String> ans = new ArrayList<>();
        for(MultipartFile file:files){
            String s = this.localStore(file);
            ans.add(s);
        }
        return ans;
    }

    private String generateRelativePath(MultipartFile file){
        String filename = file.getOriginalFilename();

        if(Strings.isNullOrEmpty(filename)){
            throw new NullPointerException("文件名为空");
        }
//        获取日期
        LocalDate date = LocalDate.now();
        int year = date.getYear();
        Month month = date.getMonth();
        int day = date.getDayOfMonth();


//        获取文件类型
        String contentType = file.getContentType();
        String type;
        if(Strings.isNullOrEmpty(contentType)){
            type="other";
        }else{
            type=contentType.split("/")[0];
        }

        String suffix = FileUtils.getSuffix(filename);
        String newFileName=UUID.randomUUID()+"."+suffix;

        return String.format("/%s/%s/%s/%s/%s",type,year,month,day,newFileName);
    }
    @Override
    public String generateStorePath(MultipartFile file) {


//        静态资源路径+相对路径
        return basePath+generateRelativePath(file);
    }

    @Override
    public boolean isAllow(String fileType) {
        if( Strings.isNullOrEmpty(fileType)){
            return false;
        }

        return !blackList.contains(fileType);
    }

    @Override
    public String generateUrl(MultipartFile file){
//        例如:/file/**
        String pattern = environment.getProperty("spring.mvc.static-path-pattern");
        String prefix="";
        if (!Strings.isNullOrEmpty(pattern)){
            prefix=pattern.replace("/**","");
        }

//        获取ip
        String ip="";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

//        获取拦截路径
        return "http://"+ip+":"+port+prefix+generateRelativePath(file);
    }
}
